package com.ternnetwork.commons.utils;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtils {
	
	
	
	
	/**
	 * 把图片画到BufferedImage中 
	 * 类似添加水印
	 * @param oriImage 要被画的图片
	 * @param file 要添加的图片
	 * @param topX
	 * @param topY
	 * @param width
	 * @param heigth
	 * @return
	 * @throws IOException
	 */
	public static BufferedImage drawImage(BufferedImage oriImage,String file,int  topX, int topY , int width,int heigth) throws IOException {
		
		BufferedImage wartermarkBufferedImage=getPngImage(file);
		
		Graphics2D videoBufferedImageG2D = oriImage.createGraphics();                
		// 设置对线段的锯齿状边缘处理             
		videoBufferedImageG2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,  RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
  	       
		// g.drawImage(img, 水印位置的横坐标, 水印位置的纵坐标,水印宽度, 水印高度, null);
		videoBufferedImageG2D.drawImage(wartermarkBufferedImage, topX, topY , width, heigth, null); 
	    
		return oriImage;
	}
	
	
    public static  BufferedImage getPngImage(String file) throws IOException {
    	Image srcImage = ImageIO.read(new File(file));
		BufferedImage srcBufferedImage = new BufferedImage(srcImage.getWidth(null),srcImage.getHeight(null), BufferedImage.TRANSLUCENT);
		Graphics2D srcGraphics2D = srcBufferedImage.createGraphics();
   		srcGraphics2D.drawImage(srcImage.getScaledInstance(srcImage.getWidth(null),srcImage.getHeight(null), Image.SCALE_SMOOTH), 0, 0,null);
        return srcBufferedImage;
    }
    

}
